package com.fenghongzhang.day008;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main4Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);

        //判断客户端的版本号
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            //23,6.0动态请求权限.1,权限数组,2代号
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},888);
        }


    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //同意
        if(requestCode == 888 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            Toast.makeText(this, "ok", Toast.LENGTH_SHORT).show();

        }else{
            Toast.makeText(this, "不ok", Toast.LENGTH_SHORT).show();
        }
    }

    public void writesd(View view) {

        /**
         * 0,授权 -清单. 2,动态授权
         * 1,判断sd卡是否可用
         * 2.路径  /storage/emulated/0
         *
         */
        //sk是否可用
        String externalStorageState = Environment.getExternalStorageState();
        if(externalStorageState.equals(Environment.MEDIA_MOUNTED)){ //判断sd是否可用
            //sk的文件路径
            File file = Environment.getExternalStorageDirectory();
            try {
                // /storage/emulated/0/abc.txt
                FileOutputStream fileOutputStream = new FileOutputStream(new File(file, "abc.txt"));
                fileOutputStream.write("哈哈哈哈".getBytes());
                fileOutputStream.flush();
                fileOutputStream.close();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }



    }

    public void readsd(View view) {
        String externalStorageState = Environment.getExternalStorageState();
        if(externalStorageState.equals(Environment.MEDIA_MOUNTED)) { //判断sd是否可用
            //sd的文件路径
            File file = Environment.getExternalStorageDirectory();
            try {
                FileInputStream fileInputStream = new FileInputStream(new File(file,"abc.txt"));

                StringBuilder stringBuilder = new StringBuilder();

                int len = 0;
                byte[] b = new byte[1024];
                while((len = fileInputStream.read(b)) != -1){
                    String s = new String(b, 0, len);
                    stringBuilder.append(s);
                }
                String string = stringBuilder.toString();
                Toast.makeText(this, string, Toast.LENGTH_SHORT).show();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
